package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.CustomerManagerService;
import com.woniu.p2p.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * UserManageController
 *
 * 用户管理
 */
@Controller
@RequestMapping("/userManage")
public class CustomerManagerController {

    @Autowired
    CustomerManagerService userManageService;

    @RequestMapping("/getAllUser")
    public String getAllUser(Map map,String name,Integer pageNum){
        Integer start=pageNum==null?1:pageNum;
        PageInfo<User> users = userManageService.findUserByName(name,start);

        map.put("users",users);
        map.put("name",name);
        return "content/customerManager";
    }


    //导出顾客信息
    @RequestMapping("/exportCustomer")
    public void exportCustomer(String name, HttpServletResponse response) throws IOException {

        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");

        List<User> list = userManageService.lookListUsersByName(name);

        ExcelWriterBuilder write = EasyExcel.write(outputStream,User.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();

    }


}
